import requests
import xlwt
from lxml import etree


f = xlwt.Workbook()
sheet1 = f.add_sheet("3D", cell_overwrite_ok=True)
row0 = ["开奖日期", "期号", "个位数", "十位数", "百位数", "单选", "组选3", "组选6", "销售额", "返奖比例"]

# 写入数据
for j in range(0, len(row0)):
    sheet1.write(0, j, row0[j])

i = 1
base_url = "http://kaijiang.zhcw.com/zhcw/html/3d/list_{}.html"


def get_response(target_url):
    return requests.get(url=target_url)


def write(response):
    global i
    response_xpath = etree.HTML(response.text)
    nodes = response_xpath.xpath("//tr")
    for node in nodes[2:-1]:

        sheet1.write(i, 0, node.xpath("./td[1]/text()"))
        sheet1.write(i, 1, node.xpath("./td[2]/text()"))
        sheet1.write(i, 2, node.xpath("./td[3]/em[1]/text()"))
        sheet1.write(i, 3, node.xpath("./td[3]/em[2]/text()"))
        sheet1.write(i, 4, node.xpath("./td[3]/em[3]/text()"))
        sheet1.write(i, 5, node.xpath("./td[4]/text()"))
        sheet1.write(i, 6, node.xpath("./td[5]/text()"))
        sheet1.write(i, 7, node.xpath("./td[6]/text()"))
        sheet1.write(i, 8, node.xpath("./td[7]//text()"))
        sheet1.write(i, 9, node.xpath("./td[8]/text()"))
        i = i + 1


for j in range(1, 20):
    response = get_response(base_url.format(j))
    write(response)


f.save("3D.xls")
